package com.xjrsoft.module.app.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.Enum.DeleteMarkEnum;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.app.dto.AppVersionDto;
import com.xjrsoft.module.app.entity.XjrAppVersion;
import com.xjrsoft.module.app.service.IXjrAppVersionService;
import com.xjrsoft.module.app.vo.AppVersionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjq
 * @since 2022-08-29
 */
@RestController
@RequestMapping("/app-version")
@AllArgsConstructor
@Api(value = "/app-version", tags = "app版本管理")
public class XjrAppVersionController {


    private final IXjrAppVersionService iXjrAppVersionService;


    /**
     * 新增
     */
    @PostMapping
    @ApiOperation(value = "新增", notes = "新增")
    public Response save(@RequestBody AppVersionDto dto) {
        XjrAppVersion xjrAppVersion = BeanUtil.copy(dto, XjrAppVersion.class);
        return Response.status(iXjrAppVersionService.save(xjrAppVersion));
    }

    /**
     * 修改
     */
    @PutMapping("/{id}")
    @ApiOperation("更新")
    public Response update(@PathVariable String id, @RequestBody AppVersionDto dto) {
        XjrAppVersion xjrAppVersion = iXjrAppVersionService.getById(id);
        if (xjrAppVersion != null) {
            BeanUtil.copy(dto, xjrAppVersion);
            return Response.status(iXjrAppVersionService.updateById(xjrAppVersion));
        } else {
            return Response.notOk("数据不存在");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public Response delete(@PathVariable String id) {
        XjrAppVersion xjrAppVersion = iXjrAppVersionService.getById(id);
        if (xjrAppVersion != null) {
            return Response.status(iXjrAppVersionService.removeById(id));
        } else {
            return Response.notOk("数据不存在");
        }
    }

    @GetMapping("/list")
    @ApiOperation("获取分页列表")
    public Response getList(PageInput pageInput) {
        IPage<XjrAppVersion> page = iXjrAppVersionService.page(ConventPage.getPage(pageInput), Wrappers.<XjrAppVersion>query().lambda()
                .eq(XjrAppVersion::getDeleteMark,DeleteMarkEnum.NODELETE.getCode())
                .like(StringUtil.isNotBlank(pageInput.getKeyword()), XjrAppVersion::getVersion, pageInput.getKeyword())
                .or()
                .like(StringUtil.isNotBlank(pageInput.getKeyword()), XjrAppVersion::getName, pageInput.getKeyword()));
        return Response.ok(ConventPage.getPageOutput(page, AppVersionVo.class));
    }

    /**
     * 更据ID查询数据
     */

    @GetMapping("/{id}")
    @ApiOperation("查询单条数据")
    public Response getAppVersionById(@PathVariable String id) {
            return Response.ok (iXjrAppVersionService.getAppVersionById(id));
    }


    @GetMapping("/new")
    @ApiOperation("获取最新数据")
    public Response getNew() {
//        XjrAppVersion xjrAppVersion = iXjrAppVersionService.getOne(Wrappers.<XjrAppVersion>query().lambda()
//                .eq(XjrAppVersion::getDeleteMark,DeleteMarkEnum.NODELETE)
//                .orderByDesc(XjrAppVersion::getCreateDate)
//                .last("Limit 1"));
//        AppVersionVo appVersionVo = BeanUtil.copy(xjrAppVersion, AppVersionVo.class);

        AppVersionVo appVersionVo = iXjrAppVersionService.getNew();
        return Response.ok(appVersionVo);
    }
}
